import 'package:enjoy_plus_ohos/pages/home/components/home_list.dart';
import 'package:enjoy_plus_ohos/pages/home/components/home_nav.dart';
import 'package:enjoy_plus_ohos/utils/http.dart';
import 'package:enjoy_plus_ohos/utils/toast.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {

  // 重写，增加自己的逻辑，保证父类中的逻辑能够正常执行
  @override
  void initState() {
    // 调用父类中的 initState 方法 保证逻辑可以正常执行
    super.initState();
    // get 请求获取数据
    getData();
  }
  

  List notifyList = [ ];

  // 么么哒
  getData() async {
    try {
      final res = await http.get('/announcement');
      // print(res['data']);
      ToastUtil.showSuccess('数据获取成功');
      setState(() {
        notifyList = res['data'];
      });
      // throw '';
    } catch (e) {
      // print(e);
       ToastUtil.showError('数据获取失败');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color.fromARGB(40, 85, 145, 175),
      appBar: AppBar(
        title: const Text('享+社区'),
        backgroundColor: Colors.transparent,
        centerTitle: true,
      ),
      body: ListView(
        scrollDirection: Axis.vertical,
        padding: const EdgeInsets.all(10),
        children: [
          // 导航条
          HomeNav(),
          // 中间广告图
          Container(
            padding: EdgeInsets.all(10),
            child: GestureDetector(
              onTap: () {
                print('点击了广告图');
              },
              child: ClipRRect(
                borderRadius: BorderRadius.circular(10.0),
                child: Image.asset('assets/images/banner@2x.jpg'),
              ),
            ),
          ),
          // 社区公告
          HomeList(notifyList: notifyList,),
        ],
      ),
    );
  }
}
